//
//  AllViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/8.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "AllViewController.h"
#import "ThreeModel.h"
#import <UIImageView+WebCache.h>

@interface AllViewController ()<UIWebViewDelegate>
@property (nonatomic,retain) UITableView *mytableView;
@property (nonatomic,retain) UIButton *leftArrow;
@property (nonatomic,retain) UIButton *rightArrow;
@property (nonatomic,retain) UIScrollView *myscrollView;

@end

@implementation AllViewController

-(void)dealloc{
    [_mytitle release];
    [_leftArrow release];
    [_rightArrow release];
    [_myscrollView release];
    [_content release];
    [_modelArr release];
    [_mytableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTitle];
    [self createTableView];
    [self createWebView];
    [self createScrollView];
    [self addArrow];
    
}


// 创建头文字
- (void)createTitle{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen_width - 120, 30)];
    label.text = _mytitle;
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    [label release];
}


// 创建tableView
- (void)createTableView{
    _mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreen_width, kScreen_height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_mytableView];
}


// 添加左右箭头
- (void)addArrow{
    self.leftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftArrow.frame = CGRectMake(10, 100, 20, 20);
    [_leftArrow addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside];
    [self.mytableView addSubview:_leftArrow];
    [_leftArrow setBackgroundImage:[UIImage imageNamed:@"iconfont-zuojiantou"] forState:UIControlStateNormal];
    
    self.rightArrow = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightArrow.frame = CGRectMake(kScreen_width - 30, 100, 20, 20);
    [self.rightArrow addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside];
    [self.mytableView addSubview:self.rightArrow];
    [self.rightArrow setBackgroundImage:[UIImage imageNamed:@"iconfont-youjiantou"] forState:UIControlStateNormal];
    
}

- (void)left{
    CGFloat current =  self.myscrollView.contentOffset.x;
    if (current == 0) {
        nil;
    }else{
        current -= kScreen_width;
    }
    [UIView animateWithDuration:0.5 animations:^{
        [self.myscrollView setContentOffset:CGPointMake(current, 0)];
    } completion:nil];}


- (void)right{
    CGFloat current =  self.myscrollView.contentOffset.x;
    if (current == (self.modelArr.count -1)*kScreen_width ) {
        nil;
    }else{
        current += kScreen_width;
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.myscrollView setContentOffset:CGPointMake(current, 0)];
    } completion:nil];
    
}


// 创建scrollView  作为表头
- (void)createScrollView{
    UIScrollView * scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 200)];
    scroll.backgroundColor = [UIColor yellowColor];
    scroll.contentSize = CGSizeMake(kScreen_width * self.modelArr.count, 100);
    scroll.pagingEnabled = YES;
    for (ThreeModel *model in self.modelArr) {
        NSInteger index = [self.modelArr indexOfObject:model];
        
        UILabel *label = [[UILabel alloc] init];
        label.width = kScreen_width;
        label.x = index *kScreen_width;
        label.height = 20;
        label.y = kScreen_height - 20;
        label.text = model.desc;
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentLeft;
        [scroll addSubview:label];
        [label release];
        
        
        
        UIImageView *imageView =  [[UIImageView alloc] initWithFrame:scroll.bounds];
        imageView.x = index *kScreen_width;
        imageView.backgroundColor = [UIColor blackColor];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.url1]];
        [scroll addSubview:imageView];
        imageView.contentMode =  UIViewContentModeScaleAspectFit ;
        [imageView release];
    }
    
    self.mytableView.tableHeaderView = scroll;
    self.myscrollView = scroll;
    [scroll release];
}





// 创建webView 作为表尾部

- (void)createWebView{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height - 200 - 64)];
    webView.delegate = self;
    webView.scrollView.scrollEnabled = NO;
    [webView loadHTMLString:_content baseURL:nil];
    self.mytableView.tableFooterView = webView ;
    [webView release];
}



- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    webView.height = height;
    _mytableView.contentSize = CGSizeMake(kScreen_width, height + 200);
    NSMutableString *str = [NSMutableString string];
    [str appendString:@"var arr = document.getElementsByTagName('img');"];
    NSString *str2 = [NSString stringWithFormat:@"for(var i=0;i<arr.length;i++){arr[i].marginLeft = 100;arr[i].width = %f;}",kScreen_width -15];
    [str appendString:str2];
    [webView stringByEvaluatingJavaScriptFromString:str];
}


@end
